<?php declare(strict_types=1);

/**
 * @author      guosir
 * @copyright   2023-2025 owner
 **/

namespace Srv\Apps\Apis\Promotion\Ctl;

use Srv\Apps\Proto\DTO\Promotion\StatDeliverAddDTO;
use Srv\Apps\Proto\DTO\Promotion\StatDeliverEditDTO;
use Srv\Apps\Proto\DTO\Promotion\StatDeliverListDTO;
use Srv\Apps\Proto\VO\Promotion\StatDeliverListVO;
use Srv\Apps\Proto\VO\Promotion\StatDeliverVO;
use Srv\Libs\Common\CommArray;
use Srv\Libs\Common\CommTime;
use Srv\Libs\Frame\ControllerAbstract;
use Srv\Libs\Frame\Page;
use Srv\Libs\Storage\DataMysql;

final class StatDeliverCtl extends ControllerAbstract
{
    private ?DataMysql $DbAuto              = null;     // DB-Write



    /**
     * @return static
     * tag
     */
    final public static function tag():self
    {
        $thisObject                         = self::tagGet();
        if($thisObject instanceof self){
            $thisObject->DbAuto             = self::getDbAuto();
        }
        return $thisObject;
    }

    /**
     * @param int $sderId
     * @return array
     * getStatDeliverSrc
     */
    public function getStatDeliverSrc(int $sderId):array
    {
        if($sderId < 1) return [];
        $isFail     = false;
        $info       = $this->DbAuto->getDataOne('SELECT sder_id, sder_day_time, sder_web_money, sder_app_money, sder_create_time, sder_update_time FROM '.$this->DbAuto->getTableNameAll('stat_deliver_record').' WHERE sder_id='.$sderId.' AND sder_delete=0', $isFail);
        if($isFail || !isset($info['sder_id'])) return [];
        return $info;
    }

    /**
     * @param Page $Page
     * @param array $whereData
     * @return StatDeliverListVO|null
     * getStatDeliverList
     */
    public function getStatDeliverList(Page $Page, array $whereData): ?StatDeliverListVO
    {
        $limitString            = $Page->getLimitString();
        $whereString            = ' WHERE sder_delete=0 ';
        // 检索条件
        if(count($whereData) > 0)foreach ($whereData as $key => $val){
            switch ($key){
                case StatDeliverListDTO::_sderId:
                    $whereString    .= (strlen($whereString)>0?' AND ':'').'sder_id='. intval($val);
                    break;
                case StatDeliverListDTO::_dayTime:
                    $whereString    .= (strlen($whereString)>0?' AND ':'').'sder_day_time='. intval($val);
                    break;
            }
        }
        $isFail                 = false;
        $sql                    = 'SELECT SQL_CALC_FOUND_ROWS sder_id, sder_day_time, sder_web_money, sder_app_money, sder_create_time, sder_update_time FROM '.$this->DbAuto->getTableNameAll('stat_deliver_record').$whereString.' ORDER BY sder_day_time DESC '.$limitString;
        $list                   = $this->DbAuto->getData($sql, $isFail);
        $totalNum               = $this->DbAuto->getDataInt('SELECT FOUND_ROWS() as num', 'num');
        if($isFail) return null;
        $StatDeliverVOList      = [];
        if(CommArray::isForeach($list)){
            foreach($list as $info){
                $sderIdItem             = intval($info['sder_id']??0);
                if($sderIdItem < 1) continue;
                $StatDeliverVO          = $this->getStatDeliverVO($info);
                if(!($StatDeliverVO instanceof StatDeliverVO)) continue;
                $StatDeliverVOList[]    = $StatDeliverVO;
            }
        }
        $StatDeliverListVO        = new StatDeliverListVO();
        $StatDeliverListVO->setList($StatDeliverVOList);
        $StatDeliverListVO->setPage($Page->getPageVO($totalNum));
        return $StatDeliverListVO;
    }

    /**
     * @param array $info
     * @return StatDeliverVO|null
     * getStatDeliverVO
     */
    public function getStatDeliverVO(array $info):?StatDeliverVO
    {
        $sderId             = intval($info['sder_id']??0);
        if($sderId < 1) return null;
        $StatDeliverVO      = new StatDeliverVO();
        $StatDeliverVO->setSderId($sderId);
        $StatDeliverVO->setDayTime(intval($info['sder_day_time']));
        $StatDeliverVO->setWebMoney(intval($info['sder_web_money']));
        $StatDeliverVO->setAppMoney(intval($info['sder_app_money']));
        $StatDeliverVO->setCreateTime(intval($info['sder_create_time']));
        $StatDeliverVO->setUpdateTime(intval($info['sder_update_time']));
        return $StatDeliverVO;
    }

    /**
     * @param array $setData
     * @return int
     * statDeliverAdd
     */
    public function statDeliverAdd(array $setData):int
    {
        if(count($setData) < 1) return 0;
        $setString      = '';
        foreach ($setData as $key => $val){
            switch ($key){
                case StatDeliverAddDTO::_dayTime:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'sder_day_time='. intval($val);
                    break;
                case StatDeliverAddDTO::_webMoney:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'sder_web_money='. intval($val);
                    break;
                case StatDeliverAddDTO::_appMoney:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'sder_app_money='. intval($val);
                    break;
            }
        }
        if(strlen($setString) < 1)  return 0;
        $currTime   = CommTime::getTimeStamp(0);
        $isFail     = false;
        $sql        = 'INSERT INTO '.$this->DbAuto->getTableNameAll('stat_deliver_record').' SET '.$setString.', sder_delete=0, sder_create_time='.$currTime.', sder_update_time='.$currTime;
        $sderId     = $this->DbAuto->getDataId($sql, $isFail);
        if($isFail || $sderId < 1) return 0;
        return $sderId;
    }

    /**
     * @param int $sderId
     * @param array $setData
     * @return bool
     * statDeliverEdit
     */
    public function statDeliverEdit(int $sderId, array $setData):bool
    {
        if($sderId < 1 || count($setData) < 1) return false;
        $setString      = '';
        foreach ($setData as $key => $val){
            switch ($key){
                case StatDeliverEditDTO::_webMoney:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'sder_web_money='. intval($val);
                    break;
                case StatDeliverEditDTO::_appMoney:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'sder_app_money='. intval($val);
                    break;
            }
        }
        if(strlen($setString) < 1) return false;
        $isFail     = false;
        $currTime   = CommTime::getTimeStamp(0);
        $sql        = 'UPDATE '.$this->DbAuto->getTableNameAll('stat_deliver_record').' SET '.$setString.', sder_update_time='.$currTime.' WHERE sder_id='.$sderId.' AND sder_delete=0';
        if($this->DbAuto->getDataNum($sql, $isFail) < 1 || $isFail) return false;
        return true;
    }
}